var app = getApp();
var api = require('../../api.js');
var timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_no:0,
    exam_id:0,
    question_list:[],
    question_no:1,
    test_anwser:[],
    indicatorDots: false,
    autoplay1: false,
    interval1: 500,
    duration: 200,
    startdo_time:0,
    flag: true,
    pusher: false,
    test_time:30,
    paper_id:0,
    auth:true,
    submithide:false,
    notshow:false,
    questiontype:'',

  },

  radioChange: function (e) {
    var page =this
    console.log(e);
    console.log('radio发生change事件，携带value值为：', e.currentTarget.dataset.key)

    var question = page.data.question_list;
    var test = page.data.test_anwser;
    let value = e.currentTarget.dataset.key;
    let index = e.currentTarget.dataset.index;
    //let no = question[index].question_number;
      test.push({
        value: value,
        no: question[index].question_number
      });
    //多选
    if (question[index].answer.length>1){
      if (question[index]['checked']) {
        if (question[index]['checked'].indexOf(value) == -1) {
          question[index]['checked'].push(value);
        } else {
          splice(question[index]['checked'].indexOf(value), 1, question[index]['checked'])
        }
      } else {
        question[index]['checked'] = [];
        question[index]['checked'].push(value);
      }
    }else{
      //单选
      question[index]['checked'] = [value];
    }
    
    
    this.setData({
      test_anwser:test,
      seleted: "选中的value：" + value,
      question_list: question,
    })
    if (index < page.data.question_list.length - 1 && question[index].answer.length == 1){
      setTimeout(function () {
        page.setData({
          current_no: index + 1
        })
      }, 500)
      
    }
    if (index == page.data.question_list.length-1){
      wx.showToast({
        title: '只有这么多了',
        icon:'none'
      })
    }
  },
  currentChange:function(e){
    var page =this
    page.setData({
      current_no: e.detail.current
    })
  },

  /***分享收藏**/
  toggleShare: function () {
    var pusher = this.data.pusher;
    this.setData({
      pusher: !this.data.pusher
    })
  },
 
  /***答题卡打开关闭**/
  showcardpop: function () {
    this.setData({ flag: false })
  },

  hidecardpop: function () {
    this.setData({ flag: true })
  },

  /*** 生命周期函数--监听页面加载*/
  onLoad: function (options) {

    if(options){
      wx.setStorageSync("tmp_options", options)
      if (options.paper_id){
        this.setData({
          paper_id: options.paper_id
        })
      }
      if (options.question_id) {
        this.setData({
          question_id: options.question_id
        })
      }
      if (options.dolog_id) {
        this.setData({
          dolog_id: options.dolog_id
        })
      }
      
    }
    // this.getPaperDetail()
    // var timestamp = Date.parse(new Date());
    // timestamp = timestamp / 1000;
    // this.setData({
    //   startdo_time: timestamp
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPaperDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/test/test?paper_id=' + this.data.paper_id + '&pid=' + user.user_id,
    }
  },
  hiddenContent(){
    wx.navigateBack({
      delta:-1
    })
  },
  //填空题输入
  inputTiankong(e){
    var page = this
    var question_list = page.data.question_list
    question_list[e.currentTarget.dataset.pkey].word[e.currentTarget.dataset.wkey] = e.detail.value;
    this.setData({
      question_list: question_list
    })
  },
  //简答题输入
  inputQestion(e){
    var page = this
    var question_list = page.data.question_list
    question_list[e.currentTarget.dataset.pkey].question = e.detail.value;
    this.setData({
      question_list: question_list
    })
  },

  getPaperDetail:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.paper.question_detail,
      method: 'POST',
      data: {
        // paper_id: page.data.paper_id,
        question_id:page.data.question_id,
        dolog_id: page.data.dolog_id,
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            question_list: res.data.questions,
            paper_info: res.data.paper_info,
            // is_collect: res.data.paper_info.is_collect ? 1 : 0,
            // test_time: res.data.paper_info.timelong,
            // auth: res.data.AUTH
          })   
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },

  //收藏
  collect:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    var collect = page.data.is_collect;
    app.request({
      url: api.paper.collect_paper,
      method: 'POST',
      data: {
        paper_id: page.data.paper_id,
        question_id:page.data.question_list[page.data.current_no].id
      },
      success: function (res) {
        if (res.errcode == 0) {
          var question_list = page.data.question_list;
          question_list[page.data.current_no].is_collect = res.data;
          page.setData({
            question_list: question_list
          })
          if (res.data==0){
            wx.showToast({
              title: '取消收藏',
            })
          }else{
            wx.showToast({
              title: '收藏成功',
            })
          }
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  topay: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })

    app.request({
      url: api.paper.add_order,
      method: "POST",
      data: {
        paper_id: that.data.paper_id
      },
      success: function (response) {
        if (response.errcode == 0) {
          if (response.data.need_pay == 1) {
            that.getPay(response.data.order_id);
          } else {
            that.getPaperDetail()
          }

        } else {
          wx.showToast({
            title: response.errmsg,
            icon: 'none'
          })
        }

      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },

})